package com.fingard.dsp.bank.directbank.tfb01;

import com.caucho.hessian.client.HessianProxyFactory;
import com.client.service.DealService;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.util.TransIDHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TFB01Base extends DirectBase implements ITcpClient {
    protected String reqTime = "";

    protected static final String INSERTSEQ = "INSERT INTO REQSEQIDMAPPING(URID,BANKCODE,TRANSDATE,REQSEQID,BANKREQSEQID) values (?,?,?,?,?);";
    protected static final String SELECTSEQ = "SELECT BANKREQSEQID FROM REQSEQIDMAPPING WHERE REQSEQID=? AND BANKCODE=?;";
    private static TransIDHelper idHelper = new TransIDHelper(1, 999999999);

    protected void writeCommonHeader(XmlTextWriter xmlWriter, TFB01Header header) throws Exception {
        if (StringHelper.isNullOrEmpty(header.bsnCode)) {
            throw new Exception("未赋值bsnCode");
        }
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
        xmlWriter.writeStartElement("body");
        xmlWriter.writeStartElement("head");
        xmlWriter.writeElementString("corpCode",  bankFront.loginName);//客户号
        xmlWriter.writeElementString("canalCode", "9102");
        reqTime = Format.DateTime17Format.format(new Date());
        xmlWriter.writeElementString("sendTime", reqTime);
        xmlWriter.writeElementString("tradeName",header.bsnCode);
        xmlWriter.writeElementString("msgType",header.msgType);
        xmlWriter.writeEndElement();
    }

    protected String[] sendToBank(String p_reqStr) throws Exception {
        String[] retStr = new String[]{"", ""};
        WriteBankLogLn("1.提交银行的地址：" + bankFront.serverURL);
        WriteBankLogLn("2.提交的报文：" + p_reqStr);

        HessianProxyFactory proxyFactory = new HessianProxyFactory();
        DealService clientService = (DealService) proxyFactory.create(
                DealService.class, bankFront.serverURL);
        // 调用远程方法
        String resultString = clientService.transfer(p_reqStr);
        retStr[1] = resultString;
        WriteBankLogStep3(retStr[1]);
        return retStr;
    }

    protected String getCharset() {
        return getBankConfig().getCharset("UTF-8");
    }
    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {

    }

    protected String getTrmSeqNum() {
        Date tmpDateNow = new Date();
        return Format.Date8Format.format(tmpDateNow) + idHelper.getNextIDStr();
    }

    protected  String timestampToDate(String timestamp){
        if(StringHelper.isNullOrEmpty(timestamp)){
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(new Date(Long.parseLong(timestamp))); // 时间戳转换日期
        return dateStr;
    }
}
